package cn.fx.user.controller;

import cn.fx.basic.exception.BusinessException;
import cn.fx.basic.util.JsonResult;
import cn.fx.basic.util.PageList;
import cn.fx.user.domain.User;
import cn.fx.user.dto.RegisterDto;
import cn.fx.user.query.UserQuery;
import cn.fx.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
//接口文档的注解
@Api(value = "用户的API",description="用户相关的CRUD功能")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 接口: 查询所有
     * http://localhost:8080/user --- url
     * 动词: GET
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping //上面的结合
    @ApiOperation(value = "查询所有用户信息" ) //打在方法上，对方法做描述
    public List<User> findAll() {

        return userService.loadAll();
    }

    /**
     * 接口: 查询单个对象
     *  http://localhost:8080/user/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public User findOne(@PathVariable("id") Long id) {

        return userService.loadById(id);
    }

    /**
     * 接口: 分页查询
     * post - 参数通过json对象传递
     *
     * @RequestBody: 从请求体中获取数据赋值给userQuery
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口" )
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        try {
            return userService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 接口: 删除
     * @param id
     * @return
     *   JsonResult: 链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            userService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }

    /**
     *  http://localhost:8080/user
     * 接口: 添加和修改
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult addOrUpdate(@RequestBody User user){
        try {
            if(user.getId() == null){
                //添加操作
                userService.save(user);
            } else {
                //修改操作
                userService.update(user);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            userService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }

    /**
     * 手机验证码注册接口
     * @param dto
     * @return
     */
    @PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody RegisterDto dto){
        try {
            userService.phoneRegister(dto);
            return JsonResult.me();
        } catch(BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }
}
